package com.zbkj.front.netty.client;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.util.Date;

/**
 * @author Administrator
 */
public class SocketClient {
    public static void main(String[] args) throws Exception {
        EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
        try {
            int num = 0;
            boolean boo = true;
            Bootstrap bootstrap = new Bootstrap();

            bootstrap.group(eventLoopGroup)
                    .channel(NioSocketChannel.class)
                    .option(ChannelOption.TCP_NODELAY, true)
                    .handler(new SocketClientInitializer());
            //绑定端口
            ChannelFuture channelFuture = bootstrap.connect("192.168.0.45", 19090).sync();
            Channel channel = channelFuture.channel();
            while (boo) {

                num++;

                channel.writeAndFlush(11234 + "--" + new Date());

                try { //休眠一段时间
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                //每一条线程向服务端发送的次数
                if (num == 9) {
                    boo = false;
                }
            }
            //channel.closeFuture().sync();
        } finally {
            //关闭事件组
            eventLoopGroup.shutdownGracefully();

        }
    }
}
